/*
 *  basePattern.h
 *  opencvExample
 *
 *  Created by 真鍋 大度 on 11/03/01.
 *  Copyright 2011 Rhizomatiks. All rights reserved.
 *
 */
#pragma once
#include "ofMain.h"
#include "ptclSendData.h"
#include "ofxOpenCv.h"

class basePattern{
public:
	virtual void setup(){}
	void allocate(){
		currentColorPixels = new unsigned char [w*h*3];	
		currentGrayPixels = new unsigned char [w*h];
		currentAlternatedColorPixels = new unsigned char [w*h*3];	
		currentAlternatedGrayPixels = new unsigned char [w*h];
		
		colorLinepixels = new unsigned char [w*h*3];	
		colorAlternatedLinepixels= new unsigned char [w*h*3];		
		grayLinePixels= new unsigned char [w*h];
		grayAlternatedLinePixels= new unsigned char [w*h];
		
		currentColorImage.allocate(w, h);
		colorLineImage.allocate(w, 1);
		
	}
	
	virtual void update(){}
	void draw(){
		currentColorImage.draw(0, 0);
	}
	
	unsigned char * getCurrentGrayPixels(){
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
//				int px = i%2==0 ? w - j - 1:j;
				int px = j;
				int bri = 
				currentColorPixels[(i*w+px) * 3     ] + 
				currentColorPixels[(i*w+px) * 3  + 1] +
				currentColorPixels[(i*w+px) * 3  + 2];
				bri = bri/3.0f;
				currentGrayPixels[(i*w+j)] = bri;
			}
		}
		return currentGrayPixels;
	}
	
	//daito
	unsigned char * getCurrentAlternatedGrayPixels(){
		
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
				//                              int px = i%2==0 ? w - j - 1:j;
				int px = j;
				int bri =
				currentColorPixels[(i*w+px) * 3     ] +
				currentColorPixels[(i*w+px) * 3  + 1] +
				currentColorPixels[(i*w+px) * 3  + 2];
				bri = bri/3.0f;
				currentGrayPixels[(i*w+j)] = bri;
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				int reverseX = i%2==0 ? w - j - 1 : j;
				currentAlternatedGrayPixels[(i * w + j)] = currentGrayPixels[(i *
																			  w + reverseX)];
			}
		}
		
		return currentAlternatedGrayPixels;
	}
	
	unsigned char * getCurrentColorPixels(){
		return currentColorPixels;
	}
	
	unsigned char * getCurrentAlternatedColorPixels(){
		for (int i = 0; i < h; i++){
			for (int j = 0; j < w; j++){
//				int px = i % 2==0 ? w - j - 1: j;
//				int px = j;
				int reverseX = i%2==0 ? w - j - 1 : j;				
				currentAlternatedColorPixels[(i*w+ j) * 3     ] = currentColorPixels[(i*w + reverseX) * 3 + 0 ];
				currentAlternatedColorPixels[(i*w+ j) * 3  + 1] = currentColorPixels[(i*w + reverseX) * 3 + 1 ];
				currentAlternatedColorPixels[(i*w+ j) * 3  + 2] = currentColorPixels[(i*w + reverseX) * 3 + 2 ];
			}
		}
		return currentAlternatedColorPixels;
	}
	

	
	virtual void setupWhenPatternChanged(){}
	virtual void cleanUpPattern(){}
	
	unsigned char * getColorLineAt(int _y){
		int offsetY = _y * w; // _y段目から
		for (int i = 0; i < w; i++) {	
			colorLinepixels[i * 3 + 0] = currentColorPixels[(i+offsetY) * 3 + 0];	// r
			colorLinepixels[i * 3 + 1] = currentColorPixels[(i+offsetY) * 3 + 1];	// g
			colorLinepixels[i * 3 + 2] = currentColorPixels[(i+offsetY) * 3 + 2]; // b
		}
		return colorLinepixels;
	}

	unsigned char * getAlternatedColorLineAt(int _y){
		int offsetY = _y * w; // _y段目から
		for (int i = 0; i < w; i++) {	
			int reverseX = _y%2==0 ? w - i - 1 : i;									
			colorAlternatedLinepixels[i * 3 + 0] = currentColorPixels[(reverseX + offsetY) * 3 + 0];	// r
			colorAlternatedLinepixels[i * 3 + 1] = currentColorPixels[(reverseX + offsetY) * 3 + 1];	// g
			colorAlternatedLinepixels[i * 3 + 2] = currentColorPixels[(reverseX + offsetY) * 3 + 2]; // b
		}
		return colorLinepixels;
	}
	
	unsigned char * getGrayLineAt(int _y){
		int offsetY = _y * w; // _y段目から
		for (int i = 0; i < w; i++) {	
			grayLinePixels[i] = currentGrayPixels[(i+offsetY)];	// r
		}
		return grayLinePixels;
	}
	
	unsigned char * getAlternatedGrayLineAt(int _y){
		int offsetY = _y * w; // _y段目から
		for (int i = 0; i < w; i++) {	
			int reverseX = _y%2==0 ? w - i - 1 : i;									
			grayLinePixels[i] = currentGrayPixels[(reverseX + offsetY)];	// r
		}
		return grayLinePixels;
	}	
	
	
	void setColorIndex(ofColor * _col, int _x,int  _y){
		ofColor * col = _col;
		int j = _x;
		int i = _y;
		col->r = currentColorPixels[(i*w+j)*3 + 0];
		col->g = currentColorPixels[(i*w+j)*3 + 1];
		col->b = currentColorPixels[(i*w+j)*3 + 2];		
		//		col.r = 
	}
	
	ofPixels * patternPixels;	
	ofxCvColorImage currentColorImage;
	ofxCvColorImage colorLineImage;	
	
	unsigned char	* colorLinepixels;		
	unsigned char	* grayLinePixels;

	unsigned char	* colorAlternatedLinepixels;		
	unsigned char	* grayAlternatedLinePixels;
	
	unsigned char 	* currentColorPixels;
	unsigned char	* currentGrayPixels; 

	//一列ずつ反対になっているマトリクス
	unsigned char 	* currentAlternatedColorPixels;
	unsigned char	* currentAlternatedGrayPixels;
	
	static const int w = PIXELSIZE;
	static const int h = CHECKPOINTNUM;	
	static const float maxGray = 250;		
	

};